from django.core.exceptions import MiddlewareNotUsed
import logging
from jboss_observer.analyzer.views import jboss_instances
from jboss_observer.analyzer.models import Jboss_Instance
from jboss_observer.analyzer.models import createReport
from jboss_observer.analyzer.models import sched

logger = logging.getLogger(__name__)
try:
    from apscheduler.scheduler import SchedulerAlreadyRunningError
    import copy
    logger.info("Loading jboss instances information from database..")
    instances = Jboss_Instance.objects.all()
    sched.start()
    for instance in instances:
        jboss_instances[instance.pk] = instance
        sched.add_interval_job(createReport, minutes=instance.track_period,args=[instance.pk],name=instance.pk) 
    logger.info("Loading jboss instances information completed.")
except MiddlewareNotUsed:
    print 'MiddlewareNotUsed2'
except SchedulerAlreadyRunningError:
    print 'scheduler already running'
